package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/17 14:00
 */
//后台系统-->图形报表展示
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService{
    //注入memberDao
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /*** 获得运营统计数据
     *Map数据格式：
     * todayNewMember ‐> number
     * totalMember ‐> number
     * thisWeekNewMember ‐> number
     * thisMonthNewMember ‐> number
     * todayOrderNumber ‐> number
     * todayVisitsNumber ‐> number
     * thisWeekOrderNumber ‐> number
     * thisWeekVisitsNumber ‐> number
     * thisMonthOrderNumber ‐> number
     * thisMonthVisitsNumber ‐> number
     * hotSetmeal ‐> List<Setmeal>
     */

    /*
    后台系统-->图形报表展示
     */
    @RequestMapping("/getBusinessReport")
    public Map<String,Object> getBusinessReport() throws Exception{
        //获取当前日期
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一天的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);
       /* //今日新增会员数
        Integer todayNewMember = memberDao.findMemberCountBeforeDate(reportDate);*/
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);

        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);

        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐(展示前四)
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        //将查询封装到map,返回
        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",reportDate);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);

        return result;
    }
}
